
let rollExecute = false
let rollInterval = null
let pitchExecute = false
let pitchInterval = null
const step = 0.5
export default {
  namespaced: true, // 启用命名空间
  state: {
    rollAngle: 50, // 左舵面上偏的角度
    elevAngle: 50 // 升降舵上偏的角度
  },
  mutations: {
    rollIncrement(state) {
      if (rollExecute) {
        return
      }
      // 清除当前定时器
      if (rollInterval !== null) {
        clearInterval(rollInterval)
        rollInterval = null
      }

      rollExecute = true
      rollInterval = setInterval(() => {
        if (state.rollAngle + step <= 100) {
          state.rollAngle += step
        } else {
          rollExecute = false
          if (rollInterval !== null) {
            clearInterval(rollInterval)
            rollInterval = null
          }
        }
      }, 50)
    },
    rollDecrement(state) {
      if (rollExecute) {
        return
      }
      // 清除当前定时器
      if (rollInterval !== null) {
        clearInterval(rollInterval)
        rollInterval = null
      }

      rollExecute = true
      rollInterval = setInterval(() => {
        if (state.rollAngle - step >= 0) {
          state.rollAngle -= step
        } else {
          rollExecute = false
          if (rollInterval !== null) {
            clearInterval(rollInterval)
            rollInterval = null
          }
        }
      }, 50)
    },
    rollStop() {
      rollExecute = false
      if (rollInterval !== null) {
        clearInterval(rollInterval)
        rollInterval = null
      }
    },

    pitchDecrement(state) {
      if (pitchExecute) {
        return
      }
      // 清除当前定时器
      if (pitchInterval !== null) {
        clearInterval(pitchInterval)
        pitchInterval = null
      }

      pitchExecute = true
      pitchInterval = setInterval(() => {
        if (state.elevAngle - step >= 10) {
          state.elevAngle -= step
        } else {
          // Directly call pitchStop here without passing state
          pitchExecute = false
          if (pitchInterval !== null) {
            clearInterval(pitchInterval)
            pitchInterval = null
          }
        }
      }, 50)
    },
    pitchIncrement(state) {
      if (pitchExecute) {
        return
      }
      // 清除当前定时器
      if (pitchInterval !== null) {
        clearInterval(pitchInterval)
        pitchInterval = null
      }

      pitchExecute = true
      pitchInterval = setInterval(() => {
        if (state.elevAngle + step <= 90) {
          state.elevAngle += step
        } else {
          pitchExecute = false
          if (pitchInterval !== null) {
            clearInterval(pitchInterval)
            pitchInterval = null
          }
        }
      }, 50)
    },
    pitchStop(state) {
      pitchExecute = false
      if (pitchInterval !== null) {
        clearInterval(pitchInterval)
        pitchInterval = null
      }
    }
  },
  actions: {
    rollIncrement({ commit }) {
      commit('rollIncrement')
    },
    rollDecrement({ commit }) {
      commit('rollDecrement')
    },
    rollStop({ commit }) {
      commit('rollStop')
    },
    pitchDecrement({ commit }) {
      commit('pitchDecrement')
    },
    pitchIncrement({ commit }) {
      commit('pitchIncrement')
    },
    pitchStop({ commit }) {
      commit('pitchStop')
    }
  },
  getters: {
    rollAngle: (state) => state.rollAngle,
    elevAngle: (state) => state.elevAngle
  }
}
